package graph;

import java.util.HashMap;


public class TextGraphBuilder extends GraphBuilder<String,Object> {
	private HashMap<String, Node<String>> nodeMap = new HashMap<String, Node<String>>();
	@Override
	protected void process(String line) {
		String[] tab = line.split(",");
		for (String e : tab) {
			String[] names = e.split("-");
			Node<String> prev = get(names[0]);
			for (int i=1 ; i<names.length ; i++) {
				Node<String> curr = get(names[i]);
				Edge<String,Object> edge = new Edge<String,Object>(prev, curr, null);
				edges.add(edge);
				prev = curr;
			}
		}
	}
	private Node<String> get(String name) {
		Node<String> node = nodeMap.get(name);
		if (node == null) { 
			node = new Node<String>(name);
			nodeMap.put(name, node);
			nodes.add(node);
		}
		return node;
	}

}
